package com.zc.im.common.config;

import com.zc.im.common.properties.HbaseProperties;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.io.IOException;
import java.util.Map;

/**
 * Project：im
 * Date：2022/4/13
 * Time：16:10
 * Description：hbase自动配置
 *
 * @author Challen.Zhang
 * @version 1.0
 */
@EnableConfigurationProperties(HbaseProperties.class)
@org.springframework.context.annotation.Configuration
public class HbaseConfiguration {

    private final HbaseProperties prop;

    public HbaseConfiguration(HbaseProperties properties) {
        this.prop = properties;
    }

    @Bean
    public Configuration configuration() {
        Configuration configuration = HBaseConfiguration.create();
        Map<String, String> config = prop.getConfig();
        config.forEach(configuration::set);
        return configuration;
    }

    @Bean
    public Connection getConnection(Configuration configuration) throws IOException {
        return ConnectionFactory.createConnection(configuration);
    }

}
